package com.example.j20240317.demos.web.controller;

import com.example.j20240317.demos.web.model.UserInfo;
import com.example.j20240317.demos.web.service.UserService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

import static com.example.j20240317.demos.web.constants.Constants.USER_SESSION_KEY;

//@RestController
//@Controller
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public boolean login(String userName, String password, HttpSession session){
        if(!StringUtils.hasLength(userName)||!StringUtils.hasLength(password)){
            return false;
        }

        UserInfo userInfo = userService.queryByName(userName);
        if(userInfo==null){
            return false;
        }
        if(password.equals(userInfo.getPassword()))
        {
            userInfo.setPassword("");
            //判断数据库的密码和用户输入的密码是否一样
            session.setAttribute(USER_SESSION_KEY,userInfo);
            return true;
        }

        return false;
    }
}
